package com.exploressquare.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.exploressquare.R;
import com.exploressquare.search.FourSquareCategories;

public class SearchAroundMe extends Activity implements TextWatcher {

	private TextView travelTextValue;
	private RatingBar distRatingBar;
	private TextView distRatingValue;
	private RatingBar reviewRatingBar;
	private TextView reviewRatingValue;
	private RatingBar popRatingBar;
	private TextView popRatingValue;
	private Spinner spinner;

	private Button searchAroundFindIt;

	private int distanceWeight = 1;
	private int reviewWeight = 1;
	private int popularityWeight = 1;
	private String travelMode;

	// private Button btnSubmit;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_around_me);

		String[] categories = loadCategories("en");

		AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoSuggest);

		textView.addTextChangedListener(this);

		textView.setThreshold(1);
		textView.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, categories));

		addListenerOnRatingBars();
		addItemsOnSpinner();
		alterPreferencesVisible(false);
		addListenerOnChkBox();

	}

	private ArrayList<String> getButtonStrings(String categoryType) {
		if (categoryType.equals("eat")) {
			return FourSquareCategories.getEatingMembers();
		}
		return null;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_search_around_me, menu);
		return true;
	}

	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub

	}

	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub

	}

	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub

	}

	private String[] loadCategories(String locale) {
		String[] returnArray = getResources().getStringArray(
				R.array.enCategories);
		return returnArray;
	}

	private void addListenerOnRatingBars() {

		travelTextValue = (TextView) findViewById(R.id.travelModeText);

		distRatingBar = (RatingBar) findViewById(R.id.searchAroundRatingDist);
		distRatingValue = (TextView) findViewById(R.id.searchAroundDistText);
		distRatingBar.setStepSize(1.0f);
		distRatingBar.setRating(1.0f);
		// if rating value is changed,
		// display the current rating value in the result (textview)
		// automatically
		distRatingBar
				.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
					public void onRatingChanged(RatingBar ratingBar,
							float rating, boolean fromUser) {

						distRatingValue.setText(getDistRatingText(rating));

					}
				});

		reviewRatingBar = (RatingBar) findViewById(R.id.searchAroundRatingRev);
		reviewRatingValue = (TextView) findViewById(R.id.searchAroundtextRev);
		reviewRatingBar.setStepSize(1.0f);
		reviewRatingBar.setRating(1.0f);
		// if rating value is changed,
		// display the current rating value in the result (textview)
		// automatically
		reviewRatingBar
				.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
					public void onRatingChanged(RatingBar ratingBar,
							float rating, boolean fromUser) {

						reviewRatingValue.setText(getReviewRatingText(rating));

					}
				});

		popRatingBar = (RatingBar) findViewById(R.id.searchAroundRatingPop);
		popRatingValue = (TextView) findViewById(R.id.searchAroundtextPop);
		popRatingBar.setStepSize(1.0f);
		popRatingBar.setRating(1.0f);
		// if rating value is changed,
		// display the current rating value in the result (textview)
		// automatically
		popRatingBar
				.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
					public void onRatingChanged(RatingBar ratingBar,
							float rating, boolean fromUser) {
						popRatingValue.setText(getPopularityRatingText(rating));
					}
				});

		searchAroundFindIt = (Button) findViewById(R.id.searchAroundFindIt);
		searchAroundFindIt.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
					
				Intent goToNextActivity = new Intent(getApplicationContext(),
						MainActivity.class);
				
				goToNextActivity.putExtra("distanceWeight", distanceWeight);
				goToNextActivity.putExtra("reviewWeight", reviewWeight);
				goToNextActivity.putExtra("popularityWeight", popularityWeight);
				startActivity(goToNextActivity);
				
				
			}
		});
	}

	private String getDistRatingText(float rating) {
		if (rating < 2.0) {
			return "Distance - No Preference";
		}
		if (rating < 3.0) {
			return "Distance - Preferably Close";
		}
		if (rating < 4.0) {
			return "Distance - Should be Close";
		}
		if (rating < 5.0) {
			return "Distance - Must be Close";
		} else
			return "Distance - Really Must be Close!";

	}

	private String getReviewRatingText(float rating) {
		if (rating < 2.0) {
			return "Review - No Preference";
		}
		if (rating < 3.0) {
			return "Review - Preferably Good";
		}
		if (rating < 4.0) {
			return "Review - Should be Good";
		}
		if (rating < 5.0) {
			return "Review - Must be Good";
		} else
			return "Review - Really Must be Good!";

	}

	private String getPopularityRatingText(float rating) {
		if (rating < 2.0) {
			return "Popularity - No Preference";
		}
		if (rating < 3.0) {
			return "Popularity - Preferably Popular";
		}
		if (rating < 4.0) {
			return "Popularity - Should be Popular";
		}
		if (rating < 5.0) {
			return "Popularity - Must be Popular";
		} else
			return "Popularity - Really Must be Popular!";

	}

	private void addItemsOnSpinner() {

		spinner = (Spinner) findViewById(R.id.spinnerTravelmode);
		List<String> list = new ArrayList<String>();
		list.add("by foot");
		list.add("by bike");
		list.add("by car");
		list.add("by public transport");
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list);

		dataAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(dataAdapter);
	}

	private void alterPreferencesVisible(boolean show) {

		if (show == true) {
			travelTextValue.setVisibility(View.VISIBLE);
			distRatingBar.setVisibility(View.VISIBLE);
			distRatingValue.setVisibility(View.VISIBLE);
			reviewRatingBar.setVisibility(View.VISIBLE);
			reviewRatingValue.setVisibility(View.VISIBLE);
			popRatingBar.setVisibility(View.VISIBLE);
			popRatingValue.setVisibility(View.VISIBLE);
			spinner.setVisibility(View.VISIBLE);
		} else {
			travelTextValue.setVisibility(View.INVISIBLE);
			distRatingBar.setVisibility(View.INVISIBLE);
			distRatingValue.setVisibility(View.INVISIBLE);
			reviewRatingBar.setVisibility(View.INVISIBLE);
			reviewRatingValue.setVisibility(View.INVISIBLE);
			popRatingBar.setVisibility(View.INVISIBLE);
			popRatingValue.setVisibility(View.INVISIBLE);
			spinner.setVisibility(View.INVISIBLE);
		}
	}

	public void addListenerOnChkBox() {

		CheckBox chkBox = (CheckBox) findViewById(R.id.showPreferences);

		chkBox.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// is chkIos checked?
				if (((CheckBox) v).isChecked()) {
					alterPreferencesVisible(true);
				} else {
					alterPreferencesVisible(false);
				}

			}
		});

	}

	public int getDistanceWeight() {
		return distanceWeight;
	}

	public void setDistanceWeight(int distanceWeight) {
		this.distanceWeight = distanceWeight;
	}

	public int getReviewWeight() {
		return reviewWeight;
	}

	public void setReviewWeight(int reviewWeight) {
		this.reviewWeight = reviewWeight;
	}

	public int getPopularityWeight() {
		return popularityWeight;
	}

	public void setPopularityWeight(int popularityWeight) {
		this.popularityWeight = popularityWeight;
	}

	
	

}
